package com.twu.biblioteca;

import com.twu.biblioteca.command.*;

import java.util.*;

public class BibliotecaApp {

    Library library;
    Map<String,Command> menu;
    List<User> users;
    User currentUser;

    static final String PAGING = "***********************************";

    public Map<String, Command> getMenu() {
        return menu;
    }

    public BibliotecaApp() {
        library = new Library();
        initializeMenu();
        initializeUsers();
    }

    private void initializeUsers() {
        users = new ArrayList<User>();
        users.add(new User("123-3456","111111","Jiaji","jiaji@email.com","12332211"));
        users.add(new User("123-4567","222222","mike","mike@email.com","123211223"));
        currentUser = null;
    }

    private Map<String,Command> initializeMenu() {
        menu = new HashMap<String, Command>();
        menu.put("sb",new ShowBooks());
        menu.put("sm",new ShowMovies());
        menu.put("c",new Checkout());
        menu.put("r",new Return());
        menu.put("h",new Help());
        menu.put("l",new Login());
        return menu;
    }

    public String welcomePage() {
        String welcome = "";
        welcome += "***********************************\n";
        welcome += "*         Biblioteca App          *\n";
        welcome += "*            Welcome!             *\n";
        welcome += "***********************************\n";
        return welcome;
    }

    public String menuPage() {
        String menuPage = "Option Menu:\n";
        for(String key:this.menu.keySet()){
            menuPage += "("+ key + ")" + menu.get(key).getName() + "\n";
        }
        menuPage += "Quit\n";
        return menuPage;
    }

    public boolean isCommandValid(String command){
        return menu.containsKey(command);
    }

    public Library getLibrary() {
        return library;
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public List<User> getUsers() {
        return users;
    }

    public String input() {
        Scanner scanIn = new Scanner(System.in);
        String option = scanIn.nextLine();
        return option;

    }

    public void run() {
        System.out.println(welcomePage());
        System.out.println(menuPage()+ PAGING);
        String option = "";
        while (true){
            option = input();
            if(option.equals("Quit")){
                break;
            }
            if(!isCommandValid(option)){
                System.out.println("Invalid Command, Please try again!\n"+PAGING);
                continue;
            }
            String page = menu.get(option).doExecute(this);
            System.out.println(page+PAGING);
        }
        System.out.println("Bye Bye!");
    }

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp();
        app.run();
    }
}
